{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "aa0280e2",
   "metadata": {},
   "source": [
    "# 通用文字识别\n",
    "\n",
    "* 接口描述\n",
    "> 在通用文字识别的基础上，提供更高精度的识别服务，支持更多语种识别（丹麦语、荷兰语、马来语、瑞典语、印尼语、波兰语、罗马尼亚语、土耳其语、希腊语、匈牙利语、泰语、越语、阿拉伯语、印地语及部分中国少数民族语言），并将字库从1w+扩展到2w+，能识别所有常用字和大部分生僻字。\n",
    "\n",
    "* 请求示例\n",
    "\n",
    "* HTTP 方法：POST\n",
    "\n",
    "> 请求URL： https://aip.baidubce.com/rest/2.0/ocr/v1/accurate_basic\n",
    "\n",
    "* 请求参数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "4ba5cb1f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'refresh_token': '25.0cc2583940173fe29defd76c960bc7ee.315360000.1965039004.282335-25946237', 'expires_in': 2592000, 'session_key': '9mzdDAepI1ZRuYIvzk9IDRpRqAnz34ty1a5tK0n/rWxEAnvfW28D5K3xRIhYbV7wkUGaghJfhXjJjsB9+fYapRM8sL7eIA==', 'access_token': '24.72d87fe3168227e57808984c34734fe8.2592000.1652271004.282335-25946237', 'scope': 'public vis-ocr_ocr brain_ocr_scope brain_ocr_general brain_ocr_general_basic vis-ocr_business_license brain_ocr_webimage brain_all_scope brain_ocr_idcard brain_ocr_driving_license brain_ocr_vehicle_license vis-ocr_plate_number brain_solution brain_ocr_plate_number brain_ocr_accurate brain_ocr_accurate_basic brain_ocr_receipt brain_ocr_business_license brain_solution_iocr brain_qrcode brain_ocr_handwriting brain_form brain_ocr_passport brain_ocr_vat_invoice brain_numbers brain_ocr_business_card brain_ocr_train_ticket brain_ocr_taxi_receipt vis-ocr_household_register vis-ocr_vis-classify_birth_certificate vis-ocr_台湾通行证 vis-ocr_港澳通行证 vis-ocr_机动车购车发票识别 vis-ocr_机动车检验合格证识别 vis-ocr_车辆vin码识别 vis-ocr_定额发票识别 vis-ocr_保单识别 vis-ocr_机打发票识别 vis-ocr_行程单识别 brain_ocr_vin brain_ocr_quota_invoice brain_ocr_birth_certificate brain_ocr_household_register brain_ocr_HK_Macau_pass brain_ocr_taiwan_pass brain_ocr_vehicle_invoice brain_ocr_vehicle_certificate brain_ocr_air_ticket brain_ocr_invoice brain_ocr_insurance_doc brain_formula brain_seal brain_ocr_facade brain_ocr_meter brain_doc_analysis brain_ocr_webimage_loc brain_ocr_medical_paper brain_ocr_doc_analysis_office brain_vat_invoice_verification brain_ocr_medical_detail brain_vehicle_registration_certificate brain_ocr_online_taxi_itinerary brain_ocr_multi_idcard brain_ocr_mixed_multi_vehicle brain_ocr_weigth_note brain_ocr_ multiple_invoice wise_adapt lebo_resource_base lightservice_public hetu_basic lightcms_map_poi kaidian_kaidian ApsMisTest_Test权限 vis-classify_flower lpq_开放 cop_helloScope ApsMis_fangdi_permission smartapp_snsapi_base smartapp_mapp_dev_manage iop_autocar oauth_tp_app smartapp_smart_game_openapi oauth_sessionkey smartapp_swanid_verify smartapp_opensource_openapi smartapp_opensource_recapi fake_face_detect_开放Scope vis-ocr_虚拟人物助理 idl-video_虚拟人物助理 smartapp_component smartapp_search_plugin avatar_video_test b2b_tp_openapi b2b_tp_openapi_online', 'session_secret': '21d2a30fe3b8d8eddcd622ff6ff02b31'}\n"
     ]
    }
   ],
   "source": [
    "# 获取鉴权机制\n",
    "\n",
    "# encoding:utf-8\n",
    "import requests \n",
    "\n",
    "# client_id 为官网获取的AK， client_secret 为官网获取的SK\n",
    "host = 'https://aip.baidubce.com/oauth/2.0/token?'\n",
    "\n",
    "# 准备URL参数\n",
    "payload = {\n",
    "    'grant_type':'client_credentials',\n",
    "    'client_id':'3AM7wN796KTfknKS5bzDMQ2y',\n",
    "    'client_secret':'qe7PZDnMbLIR6Eiwn3s9rgGzVD7k2Emo'\n",
    "}\n",
    "\n",
    "response = requests.get(host,params=payload)\n",
    "if response:\n",
    "    print(response.json())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "2c4a283c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'24.72d87fe3168227e57808984c34734fe8.2592000.1652271004.282335-25946237'"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "access_token = response.json()['access_token']\n",
    "access_token"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "310372ec",
   "metadata": {},
   "source": [
    "## 通用文字识别"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "f2fc8d3c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'words_result': [{'words': '日报账明细'}, {'words': '日期：11.23制单人：迟世蕾'}, {'words': 'No.分类'}, {'words': '收入'}, {'words': '支出'}, {'words': '类目'}, {'words': '金额'}, {'words': '凭证号'}, {'words': '类且'}, {'words': '金额'}, {'words': '凭证号'}, {'words': '1'}, {'words': '11.20果蔬/肉/蛋'}, {'words': '2444.60'}, {'words': '2'}, {'words': '11.21果蔬/肉/米'}, {'words': '1035.60'}, {'words': '11-28'}, {'words': '3'}, {'words': '11.22果蔬/蛋/肉'}, {'words': '818.40'}, {'words': '4'}, {'words': '11.23肉/果蔬/米面'}, {'words': '11-29'}, {'words': '1829.00'}, {'words': '5'}, {'words': '笔记本/胶带/电池/剪刀等'}, {'words': '203.50'}, {'words': '11-30'}, {'words': '6'}, {'words': '7'}, {'words': '8'}, {'words': '9'}, {'words': '10'}, {'words': '11'}, {'words': '12'}, {'words': '13'}, {'words': '合计'}, {'words': '6,331.10'}, {'words': '审核人：'}, {'words': '市核人37'}], 'words_result_num': 41, 'log_id': 1513489933252197298}\n"
     ]
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "通用文字识别（高精度版）\n",
    "'''\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/ocr/v1/accurate_basic\"\n",
    "# 二进制方式打开图片文件\n",
    "f = open('1.jpg', 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "params = {\"image\":img}\n",
    "access_token = '24.72d87fe3168227e57808984c34734fe8.2592000.1652271004.282335-25946237'\n",
    "request_url = request_url + \"?access_token=\" + access_token\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "if response:\n",
    "    print (response.json())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "62bba01c",
   "metadata": {},
   "source": [
    "## 网络图片文字识别"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "fc6333e3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'words_result': [{'words': '人脸技术'}, {'words': '图像生成'}, {'words': '图像处理'}, {'words': '图像识别'}, {'words': '人体技术'}, {'words': '头部分割'}, {'words': '新海诚动漫风'}, {'words': '高清仿妆'}, {'words': '宝宝检测'}, {'words': '人体外轮廓点检测'}, {'words': '宏观人脸分析'}, {'words': '绘画机器人'}, {'words': '美颜技术'}, {'words': '图像分类'}, {'words': '精细化人像抠图'}, {'words': '人脸关键点检测'}, {'words': '动漫化身'}, {'words': '美妆技术'}, {'words': '图像深度估计'}, {'words': '人体框检测'}, {'words': '微观人脸分析'}, {'words': '像素风'}, {'words': '美型技术'}, {'words': '天空分割'}, {'words': '肢体关键点检测'}, {'words': '头发分析'}, {'words': '超清人像'}, {'words': '百变滤镜'}, {'words': '食物分割'}, {'words': '人像分割'}, {'words': '五官分割'}, {'words': '图像融合'}, {'words': '图像去噪'}, {'words': '猫狗脸技术'}, {'words': '服饰识别'}, {'words': '头发分割'}, {'words': '换脸技术'}, {'words': 'AR特效'}, {'words': '食物识别'}, {'words': '手势识别'}, {'words': '神奇变脸'}, {'words': '图像画质修复'}, {'words': '图像美学评分'}, {'words': '皮肤分割'}, {'words': '图像风格化'}, {'words': 'A牙齿修复'}, {'words': '证件照生成'}, {'words': '照片直播'}, {'words': '线稿'}], 'words_result_num': 49, 'log_id': 1513491217935389811}\n"
     ]
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "网络图片文字识别\n",
    "'''\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/ocr/v1/webimage\"\n",
    "# 二进制方式打开图片文件\n",
    "f = open('2.jpg', 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "params = {\"image\":img}\n",
    "access_token = '24.72d87fe3168227e57808984c34734fe8.2592000.1652271004.282335-25946237'\n",
    "request_url = request_url + \"?access_token=\" + access_token\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "if response:\n",
    "    print (response.json())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d855ed00",
   "metadata": {},
   "source": [
    "## 二维码识别"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "dddba60f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'codes_result': [{'text': ['嘻嘻，都别管我啦'], 'type': 'QR_CODE'}], 'codes_result_num': 1, 'log_id': 1513496682410473220}\n"
     ]
    }
   ],
   "source": [
    "\n",
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "二维码识别\n",
    "'''\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/ocr/v1/qrcode\"\n",
    "# 二进制方式打开图片文件\n",
    "f = open('7.jpg', 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "params = {\"image\":img}\n",
    "access_token = '24.72d87fe3168227e57808984c34734fe8.2592000.1652271004.282335-25946237'\n",
    "request_url = request_url + \"?access_token=\" + access_token\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "if response:\n",
    "    print (response.json())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ca773aea",
   "metadata": {},
   "source": [
    "## 身份证识别"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "a9382d53",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'words_result': {'姓名': {'location': {'top': 39, 'left': 96, 'width': 67, 'height': 25}, 'words': '韦小宝'}, '民族': {'location': {'top': 82, 'left': 197, 'width': 17, 'height': 20}, 'words': '汉'}, '住址': {'location': {'top': 159, 'left': 90, 'width': 219, 'height': 55}, 'words': '北京市东城区景山前街4号紫禁城敬事房'}, '公民身份号码': {'location': {'top': 0, 'left': 0, 'width': 0, 'height': 0}, 'words': ''}, '出生': {'location': {'top': 0, 'left': 0, 'width': 0, 'height': 0}, 'words': ''}, '性别': {'location': {'top': 81, 'left': 95, 'width': 19, 'height': 22}, 'words': '男'}}, 'words_result_num': 6, 'image_status': 'unknown', 'idcard_number_type': 0, 'log_id': 1513492643564960774}\n"
     ]
    }
   ],
   "source": [
    "\n",
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "身份证识别\n",
    "'''\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/ocr/v1/idcard\"\n",
    "# 二进制方式打开图片文件\n",
    "f = open('3.jpg', 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "params = {\"id_card_side\":\"front\",\"image\":img}\n",
    "access_token = '24.72d87fe3168227e57808984c34734fe8.2592000.1652271004.282335-25946237'\n",
    "request_url = request_url + \"?access_token=\" + access_token\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "if response:\n",
    "    print (response.json())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "28af9fe0",
   "metadata": {},
   "source": [
    "## 增值税发票识别"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "7225f9ed",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'words_result_num': 43, 'words_result': {'AmountInWords': '叁拾陆圆玖角柒分', 'InvoiceNumConfirm': '', 'CommodityEndDate': [], 'CommodityVehicleType': [], 'CommodityStartDate': [], 'CommodityPrice': [{'row': '1', 'word': '25.00'}], 'NoteDrawer': '京东商城', 'SellerAddress': '北京市大兴区黄村镇政府东配楼306室57807000', 'CommodityNum': [{'row': '1', 'word': '1'}], 'SellerRegisterNum': '91110115MA00GMBB48', 'MachineCode': '661620039561', 'Remarks': '订单号:81373106890', 'SellerBank': '招商银行股份有限公司北京亚运村支行110929378210802', 'CommodityTaxRate': [{'row': '1', 'word': '16%'}], 'TotalTax': '5.10', 'InvoiceCodeConfirm': '011001700111', 'CheckCode': '53031321403507561254', 'InvoiceCode': '011001700111', 'InvoiceDate': '2018年10月25日', 'PurchaserRegisterNum': '', 'InvoiceTypeOrg': '北京增值税电子普通发票', 'OnlinePay': '', 'Password': '3/<6-91/3407/>>0/7<0>463157*409451>/->3866<9<8/67-5/-18317294898<0/21+*-66/5<064/639786>9451>/->3866<9<8-><>', 'Agent': '否', 'AmountInFiguers': '36.97', 'PurchaserBank': '', 'Checker': '', 'City': '', 'TotalAmount': '31.87', 'CommodityAmount': [{'row': '1', 'word': '25.00'}, {'row': '2', 'word': '6.90'}, {'row': '3', 'word': '6.90'}], 'PurchaserName': '个人', 'CommodityType': [{'row': '1', 'word': '无'}], 'Province': '北京', 'InvoiceType': '电子普通发票', 'SheetNum': '', 'PurchaserAddress': '', 'CommodityTax': [{'row': '1', 'word': '4.00'}, {'row': '2', 'word': '0.00'}, {'row': '3', 'word': '1.10'}], 'CommodityPlateNum': [], 'CommodityUnit': [{'row': '1', 'word': '个'}, {'row': '2', 'word': '1'}], 'Payee': '京东商城', 'CommodityName': [{'row': '1', 'word': '*日用杂品*墨斗鱼银盖沙遥蓝沙10分钟1978时间计时器摆件书房书桌书架摆件送朋友孩子创意生日'}, {'row': '2', 'word': '*日用杂品*墨斗鱼银盖沙遥蓝沙10分钟1978时间计时器据件书房书桌书架摆件送朋友孩子创意生日'}, {'row': '3', 'word': '*物流辅助服务*配送费'}], 'SellerName': '北京京东金禾贸易有限公司', 'InvoiceNum': '79991540'}, 'log_id': 1513493236030345246}\n"
     ]
    }
   ],
   "source": [
    "\n",
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "增值税发票识别\n",
    "'''\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/ocr/v1/vat_invoice\"\n",
    "# 二进制方式打开图片文件\n",
    "f = open('5.jpg', 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "params = {\"image\":img}\n",
    "access_token = '24.72d87fe3168227e57808984c34734fe8.2592000.1652271004.282335-25946237'\n",
    "request_url = request_url + \"?access_token=\" + access_token\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "if response:\n",
    "    print (response.json())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5c4970ef",
   "metadata": {},
   "outputs": [],
   "source": [
    " "
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
